//
//  HQuotationViewController.m
//  Router
//
//  Created by Zhen Huang on 2018/6/26.
//  Copyright © 2018年 James. All rights reserved.
//

#import "HQuotationViewController.h"
#import "HQuotationModel.h"
#import "HQuotationTableViewCell.h"
#import "HOfficialRankModel.h"
#import "HOfficialRankTableViewCell.h"
@interface HQuotationViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabelview;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *officalRankArray;
@end

@implementation HQuotationViewController
- (instancetype)init {
    if (self) {
        self.dataArray = [NSMutableArray new];
        self.officalRankArray = [NSMutableArray new];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    [self getData];
    [self getOfficial_rankData];
//    [self startGCDTimer];
    [self.view addSubview:self.tabelview];
}
- (UITableView *)tabelview {
    if (!_tabelview) {
        _tabelview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStyleGrouped];
        _tabelview.delegate = self;
        _tabelview.dataSource = self;
        _tabelview.backgroundColor = kBackGroundColor;
        _tabelview.separatorColor = [UIColor colorWithCGColor:[kStrokeBackGroundColor CGColor]];
        [_tabelview registerClass:[HQuotationTableViewCell class] forCellReuseIdentifier:@"CellID"];
        [_tabelview registerClass:[HOfficialRankTableViewCell class] forCellReuseIdentifier:@"OffCellID"];
        _tabelview.tableFooterView = [[UIView alloc]init];
    }
    return _tabelview;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HQuotationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = kBackGroundColor;
        cell.preservesSuperviewLayoutMargins = NO;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins = UIEdgeInsetsZero;
        if (indexPath.row % 2 != 0) {
            cell.backgroundColor = kWidgetBackGroundColor;
        }
        if (indexPath.row == 0) {
            cell.nameLabel.text = @"平台名";
            cell.changeLabel.text = @"涨跌幅";
            cell.turnoverLabel.text = @"成交量";
            cell.valueLabel.text = @"成交价";
        }else {
            HQuotationModel *model = self.dataArray[indexPath.row - 1];
            cell.nameLabel.text = model.name;
            cell.changeLabel.text = model.change;
            cell.turnoverLabel.text = [NSString stringWithFormat:@"%.2f",model.turnover];
            cell.valueLabel.text = [NSString stringWithFormat:@"%.3f",model.value];
        }
        return cell;
    }else if (indexPath.section == 1) {
        HOfficialRankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OffCellID" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = kBackGroundColor;
        if (indexPath.row % 2 != 0) {
            cell.backgroundColor = kWidgetBackGroundColor;
        }
        cell.preservesSuperviewLayoutMargins = NO;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins = UIEdgeInsetsZero;
        if (indexPath.row == 0) {
            cell.NumberRowsLabel.text = @"#";
            cell.accountlabel.text = @"监工用户";
            cell.outputLabel.text = @"单台产量";
        }else {
            HOfficialRankModel *model = self.officalRankArray[indexPath.row - 1];
            cell.NumberRowsLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
            cell.accountlabel.text = model.account;;
            cell.outputLabel.text = [NSString stringWithFormat:@"%ld",model.output];
        }
        return cell;
    }
    return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.dataArray.count + 1;
    }else if (section == 1) {
        return self.officalRankArray.count + 1;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    if (section == 0) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth / 3, 20)];
        label.backgroundColor = kBackGroundColor;
        label.font = [UIFont fontWithName:PingFangSCRegular size:12];
        label.text = @"部分平台实时行情";
        label.textAlignment = 2;
        label.numberOfLines = 0;//根据最大行数需求来设置
        label.lineBreakMode = NSLineBreakByTruncatingTail;
        CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
        CGSize expectSize = [label sizeThatFits:maximumLabelSize];
        label.width = expectSize.width;
        label.textColor = kNavBlueColor;
        
        UILabel *explainLabel = [[UILabel alloc]initWithFrame:CGRectMake(label.right + 3 , 10, ScreenWidth / 2, 20)];
        explainLabel.backgroundColor = kBackGroundColor;
        explainLabel.font = [UIFont fontWithName:PingFangSCRegular size:9];
        explainLabel.text = @"(每10秒刷新，仅供参考与本站无关)";
        explainLabel.textAlignment = 0;
        explainLabel.textColor = kTitleBackGroundColor;
        [headView addSubview:label];
        [headView addSubview:explainLabel];
    }else {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth / 3, 20)];
        label.backgroundColor = kBackGroundColor;
        label.font = [UIFont fontWithName:PingFangSCRegular size:12];
        label.text = @"全网Top5产量排行榜";
        label.textAlignment = 1;
        label.textColor = kNavBlueColor;
        
        UILabel *explainLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth / 3 * 2, 10, ScreenWidth / 3, 20)];
        explainLabel.backgroundColor = kBackGroundColor;
        explainLabel.font = [UIFont fontWithName:PingFangSCRegular size:12];
        explainLabel.text = [self getNDay:0];
        explainLabel.textAlignment = 1;
        explainLabel.textColor = kTitleBackGroundColor;
        [headView addSubview:label];
        [headView addSubview:explainLabel];
    }
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (void)getData {
    NSString *url = F(@"%@/outputs/platform_quotes", BaseUrl);
    NSString *Token = F(@"Token %@",  [[TMCache sharedCache] objectForKey:@"Token"]);
    [self receivePOSTWithTokenURLString:url method:@"GET" body:nil Token:Token Blocks:^(NSDictionary *tempData, NSInteger state) {
        if (tempData) {
            self.dataArray = [HQuotationModel mj_objectArrayWithKeyValuesArray:tempData];
            [self.tabelview reloadData];
        }
    }];
}
-(void) startGCDTimer{
    // GCD定时器
    static dispatch_source_t _timer;
    NSTimeInterval period = 10.0; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    // 事件回调
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self getData];
        });
    });
    // 开启定时器
    dispatch_resume(_timer);
}
- (void)getOfficial_rankData {
    NSString *url = F(@"%@/outputs/official_rank", BaseUrl);
    NSString *Token = F(@"Token %@",  [[TMCache sharedCache] objectForKey:@"Token"]);
    [self receivePOSTWithTokenURLString:url method:@"GET" body:nil Token:Token Blocks:^(NSDictionary *tempData, NSInteger state) {
        if (tempData) {
            self.officalRankArray = [HOfficialRankModel mj_objectArrayWithKeyValuesArray:tempData];
            [self.tabelview reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
